﻿using JuCloudy.Data.Entity;
using JuCloudy.Data;
using JuCloudy.Data.Nhibernate;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace JuSolution.Web.ApiService
{
    /// <summary>
    /// WSUploadService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class WSUploadService : System.Web.Services.WebService
    {
        Random r = new Random();
        [WebMethod]
        public void ProcessRequest(int AppId, String Diary, String Path, long offSet, byte[] intoBuffer)
        {
            //指定上传文件夹+文件名(相对路径)
            string strPath = Server.MapPath("/ClientBin/" + Path);

            if (!Directory.Exists(strPath))
                Directory.CreateDirectory(strPath);

            DateTime dt = DateTime.Now;
            String FileName = timeStamp.ConvertDateTimeInt(dt).ToString() + ".xap";
            if (offSet < 0)
            {
                offSet = 0;
            }

            byte[] buffer = intoBuffer;

            if (buffer != null)
            {
                //读写文件的文件流,支持同步读写也支持异步读写
                File.WriteAllBytes(strPath + "\\" + FileName, buffer);
               /* using (FileStream filesStream
                    = new FileStream(strPath + "\\" + FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    filesStream.Seek(offSet, SeekOrigin.Begin);
                    filesStream.Write(buffer, 0, buffer.Length);
                    filesStream.Flush();
                    filesStream.Close();
                    //filesStream.Dispose();
                }*/
                SysContainer container = new SysContainer();
                container.InsertAppModule(new SysAppModule()
                {
                     AppId=AppId, CreateDate=dt, CreateDateTimestamp=timeStamp.ConvertDateTimeInt(dt),
                     Diary = Diary,
                     Filename = Path + "/" + FileName,
                     IsEnable = true
                });
            }
        }
        
    }
}
